package com.ttloner2.dao;

import com.ttloner2.bean.Customer;
import org.junit.Test;

import java.io.*;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author TTLoner
 * @date 2021/6/24 20:46
 * @description
 * 针对customer表的DAO实现类
 */
public class CustomerDAOImpl extends BaseDAO implements CustomerDAO{
    @Override
    public int insert(Connection conn, Customer customer) {
        String sql="insert into customers(id,name,email,birth,photo) values(?,?,?,?,?)";
        int result = update(conn, sql,customer.getId(),customer.getName(),customer.getEmail(),customer.getBirth(),customer.getPhoto());
        return result;
    }

    @Override
    public int deleteById(Connection conn, int id) {
        String sql="delete from customers where id=?";
        int result = update(conn, sql, id);
        return result;
    }

    @Override
    public int update(Connection conn, Customer customer) {
        String sql="update customers set id=?,name=?,email=?,birth=?,photo=?";
        int result = update(conn, sql,customer.getId(),customer.getName(),customer.getEmail(),customer.getBirth(),customer.getPhoto());
        return result;
    }

    @Override
    public Customer getCustomerById(Connection conn, int id) {
        ArrayList<Customer> instance=null;
        String sql="select * from customers where id=?";
        try {
            instance = getInstance(conn, Customer.class, sql, id);
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        if(instance.size()==0) return null;
        return instance.get(0);
    }

    @Override
    public ArrayList<Customer> getAll(Connection conn) {
        ArrayList<Customer> instance=null;
        String sql="select * from customers";
        try {
            instance = getInstance(conn, Customer.class, sql);
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return instance;
    }

    @Override
    public Long getCount(Connection conn) {
        String sql="select count(*) from customers";
        Long count = getSingleValue(conn, sql);
        return count;
    }
}
